<?php

class MoneyMode extends Data{
    /** @return MoneyMode */
    public static function init() {
        return parent::init();
    }
    //交易历史明细
    function getList($user_id = null,$event_type = null,$rows = 15,$offset = 0){
        $rows=(int)$rows;
        $offset=(int)$offset;
        $condition='';
        if(!empty($user_id)){
            $condition.=" AND (`user_id`=".(int)$user_id.")";
        }
        switch($event_type){
            case 'all':$condition.=" AND `event_type` = ' '";break;
            case 'in':$condition.=" AND `money`>=0";break;
            case 'out':$condition.=" AND `money`<0";break;
            case 'share':$condition.=" AND `event_type` = 'share'";break;
            case 'buy':$condition.=" AND `event_type` = 'buy'";break;
            case 'serviceFee':$condition.=" AND `event_type` = 'serviceFee'";break;
            case 'shopSell':$condition.=" AND `event_type` = 'shopSell'";break;

            default:$condition.='';
        }
   
        $sql="SELECT SQL_CALC_FOUND_ROWS * FROM `money_history`
              WHERE 1 AND `enable` = 1 $condition
              ORDER BY `create_time` DESC LIMIT $offset,$rows";
        return $this->db->getList($sql,$user_id);
    }
    //新的一条
    function add($user_id, $event, $money, $total, $remark, $trade_no=null,
                 $event_type=null, $pay_type=null,$enable = 0,$order_ids = null){
        $data['user_id']=$user_id;
        $data['event']=$event;
        $data['money']=$money;
        $data['total']=$total;
        $data['remark']=$remark;
        $data['trade_no']=$trade_no;
        $data['event_type']=$event_type;
        $data['pay_type']=$pay_type;
        $data['enable']=$enable;
        $data['order_ids']=$order_ids;
        return $this->db->insert('money_history',$data)==1 ? (int)$this->db->insertId():-1;
    }
    function setSuccessSymbol($id,$trade_no = null,$totalMoney,$enable = 1){
        $data['trade_no']=$trade_no;
        $data['total']=$totalMoney;
        $data['enable']=$enable;
        return $this->db->update('money_history',$id,$data,true) == 1;
    }

    //余额支付,(订单状态)
    function moneyPay($user_id,$payment_amount){
        $user_id=(int)$user_id;
        $payment_amount=(double)$payment_amount;
        $upRe=$this->updateMoney($user_id,$payment_amount,false);
        return $upRe? true:false;
    }
    //修改钱数
    function updateMoney($user_id,$money,$inc = false){
        $condition= $inc ? '+'.(double)$money : '-'.(double)$money;
        $sql="UPDATE `user_info` SET `money`=`money`".$condition." WHERE `user_id`=?";
        return $this->db->sqlExec($sql,$user_id)==1? true:false;      //user_id是user表的id
    }
    /**
     * 提现申请
     * @param $user_id
     * @param $money
     * @return int  0金额非法 x>0成功 -1失败
     */
    function applyCash($user_id,$money,$account_type,$acount_no){
        $userInfo=UserMode::init()->getDetailWithoutJoin($user_id);
        $realMmoney=$userInfo['money'];
        if((double)$money<=0.0 || $money>$realMmoney) return 0;//金额非法
        $data['user_id']=$user_id;
        $data['money']=$money;
        $data['account_type']=$account_type;
        $data['account_no']=$acount_no;
        if($this->db->insert('user_cash',$data)==1){
            if($this->updateMoney($user_id,$money,false)){
                $addHistory = $this->add($user_id,'提现',-$money,$userInfo['money'],'提现',null,'applyMoney',null,1,null);
                if($addHistory) {
                    return true;
                }
            }
        }
        return false;
    }
    function getCashDetail($id){
        $sql="SELECT * FROM `user_cash` WHERE `id`=?";
        return $this->db->getOne($sql,$id);
    }
    //后台审核提现申请(汇款之后)
    function authCash($id,$auth = '1'){
        $data['auth']=$auth;
        $data['confirm_time']=date("Y-m-d H:i:s");
        return $this->db->update('user_cash',$id,$data)==1?true:false;
    }
    function getCashList($rows = 30,$offset = 0){
        $rows=(int)$rows;
        $offset=(int)$offset;
        $sql="SELECT SQL_CALC_FOUND_ROWS * FROM `user_cash` ORDER BY `create_time` DESC LIMIT $offset,$rows";
        return $this->db->getList($sql);
    }
    function getCashListByUserId($user_id,$rows = 30,$offset = 0){
        $rows=(int)$rows;
        $offset=(int)$offset;
        $sql="SELECT SQL_CALC_FOUND_ROWS * FROM `user_cash` WHERE `user_id`=$user_id ORDER BY `create_time` DESC LIMIT $offset,$rows";
        return $this->db->getList($sql);
    }
    function getDetail($id){
        $sql="SELECT * FROM `money_history` WHERE `id`=?";
        return $this->db->getOne($sql,$id);
    }
    function getOrderIdsById($id){
        $detail=$this->getDetail($id);
        return !empty($detail)?$detail['order_ids']:null;
    }
    //history id是否存在
    function getHistoryIdOfOrderId($order_id){
        $sql="SELECT `id` FROM `money_history` WHERE `order_ids`=? AND `enable`=0";
        return $this->db->getValue($sql,$order_id);
    }
    //查询某订单的结算信息
    function getMoneyInfoByOrderId($order_id){
        $sql="SELECT * FROM `money_history` WHERE `order_ids`=? AND `enable`=1";
        return $this->db->getOne($sql,$order_id);
    }
    //查询某用户最近一次提现的类型及账号
    function getTypeAndNum($user_id){
        $sql="SELECT * FROM `user_cash` WHERE `user_id`=? ORDER BY `create_time` DESC LIMIT 1";
        return $this->db->getOne($sql,$user_id);
    }
    function getUserIdById($id){
        $detail=$this->getDetail($id);
        return empty($detail)?null:$detail['user_id'];
    }
    function getNewestHistoryOfUserId($user_id){
        $sql="SELECT * FROM `money_history` WHERE `user_id`=? AND `pay_type`='alipay' ORDER BY `create_time` DESC limit 1";
        return $this->db->getOne($sql,$user_id);
    }
    //支付成功后，检查该订单是否处理过
    function checkHistoryDealed($id){
        $info=$this->getDetail($id);
        return $info['enable']==1 ? true:false;
    }
    //如果通知了notify_url插入一条
    function addNotifyInfo($out_trade_no,$trade_no,$trade_status){
        $data['out_trade_no']=$out_trade_no;
        $data['trade_no']=$trade_no;
        $data['trade_status']=$trade_status;
        return $this->db->insert('notify_info',$data)==1 ? (int)$this->db->insertId():-1;
    }
} 